package com.boxedinsoftware.tts.callerID;

import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class IncomingCallListener extends BroadcastReceiver {
	
	private static final String TAG = "IncomingCallListener";

	@Override
	public void onReceive(Context arg0, Intent arg1) {


		TelephonyManager tel = (TelephonyManager)arg0.getSystemService(Context.TELEPHONY_SERVICE);
			CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
			Log.v(TAG, "in Receiver");
			tel.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
			
			Bundle bundle1 = arg1.getExtras();
			String phoneNumber = bundle1.getString("incoming_number");
			Log.v(TAG, phoneNumber);
			Intent myIntent = new Intent(arg0, TTSservice.class);
    	    
    		Bundle bundle = new Bundle();
    		bundle.putString("number", phoneNumber);
    		myIntent.putExtras(bundle);
    		//startService(myIntent);    
    		PendingIntent pendingIntent = PendingIntent.getService(arg0, 0, myIntent, 0);
    		try {
				pendingIntent.send();
			} catch (CanceledException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	
	}
	
	

}
